local rongai = fk.CreateSkill{
  name = "xiaobai__rongai",
}

Fk:loadTranslationTable{
  ["xiaobai__rongai"] = "荣哀",
  [":xiaobai__rongai"] = "每轮各限一次，其他角色的回合开始时，你可以将手牌数调整为1或4，回合结束时，若你本回合受到过伤害，你将手牌数调整为另一值。",
  ["xiaobai__rongai_change1"] = "调整至1张",
  ["xiaobai__rongai_change4"] = "调整至4张",

  ["$xiaobai__rongai1"] = "似雨知心，如风识路，何须世上虚名度？",
  ["$xiaobai__rongai2"] = "相逢佳时，此番约定同君住。",

}

rongai:addEffect(fk.TurnStart, {
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and target ~= player and ((player:getMark("xiaobai__rongai1-round") == 0 and player:getHandcardNum() ~= 1) or 
      player:getMark("xiaobai__rongai4-round") == 0 and player:getHandcardNum() ~= 4)
  end,
  on_cost = function (self, event, target, player, data)
    local all_choices = {"xiaobai__rongai_change1", "xiaobai__rongai_change4", "Cancel"}
    local choices = {}
    if player:getMark("xiaobai__rongai1-round") == 0 and player:getHandcardNum() ~= 1 then
      table.insert(choices,all_choices[1])
    end
    if player:getMark("xiaobai__rongai4-round") == 0  and player:getHandcardNum() ~= 4 then
      table.insert(choices,all_choices[2])
    end
    table.insert(choices,all_choices[3])
    local r = player.room:askToChoice(player, {
      choices = choices,
      skill_name = rongai.name,
      all_choices = all_choices})
    if r == "Cancel" then return end
    event:setCostData(self, {response = r})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local num
    if event:getCostData(self).response == "xiaobai__rongai_change1" then
      player.room:setPlayerMark(player, "xiaobai__rongai1-round", 1)
      player.room:setPlayerMark(player, "xiaobai__rongaic4-turn", 1)
      num = player:getHandcardNum() - 1
    else
      player.room:setPlayerMark(player, "xiaobai__rongai4-round",1)
      player.room:setPlayerMark(player, "xiaobai__rongaic1-turn",1)
      num = player:getHandcardNum() - 4
    end
    if num < 0 then
      player:drawCards(-num, rongai.name)
    elseif num> 0 then
      player.room:askToDiscard(player, {
        min_num = num,
        max_num = num,
        cancelable = false,
        skill_name = rongai.name,
        include_equip = false
      })
    end
  end
})

rongai:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self, true, true) and target ~= player and (player:getMark("xiaobai__rongaic1-turn") == 0 or player:getMark("xiaobai__rongaic4-turn")) and
     #player.room.logic:getActualDamageEvents(1, function (e)
        return e.data.to == player
      end, Player.HistoryTurn) > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local num
    if player:getMark("xiaobai__rongaic4-turn") then
      num = player:getHandcardNum() - 4
    else
      num = player:getHandcardNum() - 1
    end
    if num < 0 then
      player:drawCards(-num, rongai.name)
    elseif num> 0 then
      player.room:askToDiscard(player,{
        min_num = num,
        max_num = num,
        cancelable = false,
        skill_name = rongai.name,
        include_equip = false
      })
    end
  end
})

return rongai